/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

 /***************************************************************************
 * Name: win_mutex.c
 *
 * Purpose: implementation mutex with windows API
 *
 * Developer:
 *   wen.gu , 2022-01-21
 *
 * TODO:
 *
 ***************************************************************************/

 /******************************************************************************
 **    INCLUDES
 ******************************************************************************/
#if defined(_MSC_VER) && (!(defined(HAS_POSIX_MUTEX_API)) || (!HAS_POSIX_MUTEX_API))
#include "avp_mutex.h"

#include <windows.h>
#include <process.h>

#include "avp_macro.h"
#include "avp_log.h"
/******************************************************************************
 **    MACROS
 ******************************************************************************/

/******************************************************************************
 **    VARIABLE DEFINITIONS
 ******************************************************************************/

/******************************************************************************
 **    FUNCTION DEFINITIONS
 ******************************************************************************/
/**
 * @brief initialize a mutex instance
 * 
 * @param void
 * @return avp_mutex_t the mutex instance
 */
avp_mutex_t avp_mutex_create(void)
{ 
    PSRWLOCK srl = (PSRWLOCK)malloc(sizeof(SRWLOCK));
    if (!srl)
    {
        LOGE("alloc mutex instance failed\n");
        abort();
    }    
	InitializeSRWLock(srl);

    return (avp_mutex_t)srl;
}

/**
 * @brief destroy a mutex instance
 * 
 * @param mtx [in] mutex instance
 * @return void
 */
void avp_mutex_destroy(avp_mutex_t mtx)
{
    free(mtx);
}

/**
 * @brief do lock with mutex instance
 * 
 * @param mtx [in] mutex intance
 * @return void
 */
void avp_mutex_lock(avp_mutex_t mtx)
{  
    AcquireSRWLockExclusive((PSRWLOCK)mtx);
}

/**
 * @brief do unlock with mutex instance
 * 
 * @param mtx [in] mutex instance
 * @return void
 */
void avp_mutex_unlock(avp_mutex_t mtx)
{  
    ReleaseSRWLockExclusive((PSRWLOCK)mtx);
}

/**
 * @brief initialize a condition variable instance
 * 
 * @param void
 * @return avp_condition_variable_t condition variable instance
 */
avp_condition_variable_t avp_cv_create(void)
{ 
    PCONDITION_VARIABLE cv = (PCONDITION_VARIABLE)malloc(sizeof(CONDITION_VARIABLE));
    if (!cv)
    {
        LOGE("alloc condition variable instance failed\n");
        abort();
    }    
    InitializeConditionVariable(cv);

    return (avp_condition_variable_t)cv;

}

/**
 * @brief destroy a condition variable instance
 * 
 * @param cv [in] condition variable instance
 * @return void
 */
void avp_cv_destroy(avp_condition_variable_t cv)
{
    free(cv);
}

/**
 * @brief wait forever with condition variable instance
 * 
 * @param cv [in] condition variable instance
 * @param mtx [in] mutex instance
 * @return void
 */
void avp_cv_wait(avp_condition_variable_t cv, avp_mutex_t mtx)
{
    (void) SleepConditionVariableSRW((PCONDITION_VARIABLE)cv, (PSRWLOCK)mtx, INFINITE, 0);
}

/**
 * @brief timed wait for condition variable instance
 * 
 * @param cv [in] condition variable instance
 * @param mtx [in] mutex instance
 * @param wait_ms [in] how long to wait,
 * @return GErrc if timeout: G_ErrTimeout, if success:G_OK 
 */
GErrc avp_cv_wait_for(avp_condition_variable_t cv, avp_mutex_t mtx, GU32 wait_ms)
{
	BOOL ok = SleepConditionVariableSRW((PCONDITION_VARIABLE)cv, (PSRWLOCK)mtx, wait_ms, 0);
	return (ok ? G_OK : G_ErrTimeout);
}

/**
 * @brief to wake up one condition wait
 * 
 * @param cv [in] condition variable instance
 * @return void 
 */
void avp_cv_notify_one(avp_condition_variable_t cv)
{
     WakeConditionVariable((PCONDITION_VARIABLE)cv);
}

/**
 * @brief to wake up all condition wait
 * 
 * @param cv [in] condition variable instance
 * @return void
 */
void avp_cv_notify_all(avp_condition_variable_t cv)
{
    WakeAllConditionVariable((PCONDITION_VARIABLE)cv);
}




#endif /** end of _MSC_VER */
